
package com.snowfeel.sfmall.code.generator.response;

import com.alibaba.fastjson.JSON;
import com.snowfeel.sfmall.code.generator.enums.ResponseCode;
import com.snowfeel.sfmall.code.generator.enums.ResultCode;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author guomenghuai
 * @date 2024/8/5
 * @Description
 */
public class JsonResult<T> implements Serializable {

    private String code = "";
    private String message = "";
    private T data;

    public JsonResult() {
    }

    public JsonResult(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public JsonResult(String code) {
        this.code = code;
    }

    public JsonResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult(String code, T data) {
        this.code = code;
        this.data = data;
    }

    public JsonResult(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return Objects.equals(ResponseCode.SUCCESS.getCode(), this.code);
    }

    public static <T> JsonResult<T> create() {
        return new JsonResult();
    }

    public static <T> JsonResult<T> create(String code) {
        return new JsonResult(code);
    }

    public static <T> JsonResult<T> create(String code, String message) {
        return new JsonResult(code, message);
    }

    public static <T> JsonResult<T> create(String code, T data) {
        return new JsonResult(code, data);
    }

    public static <T> JsonResult<T> create(String code, String message, T data) {
        return new JsonResult(code, message, data);
    }

    public static <T> JsonResult<T> ok() {
        return new JsonResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }

    public static <T> JsonResult<T> success(T data) {
        return new JsonResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static <T> JsonResult<T> error(String message) {
        return new JsonResult(ResponseCode.UN_ERROR.getCode(), message);
    }

    public static <T> JsonResult<T> error(String errorCode, String message) {
        return new JsonResult(errorCode, message);
    }

    public static <T> JsonResult<T> error(ResultCode errorType) {
        return new JsonResult(errorType.getCode(), errorType.getMessage());
    }

    public static <T> JsonResult<T> error(ResultCode errorType, String message) {
        return new JsonResult(errorType.getCode(), message);
    }

    public <T> JsonResult<T> message(String message) {
        return new JsonResult(this.code, message, this.data);
    }

    public <T> JsonResult<T> body(T data) {
        return new JsonResult(this.code, this.message, data);
    }

    public <T> JsonResult<T> body(T data, Class<?> cls) {
        Field field = null;

        try {
            field = cls.getDeclaredField("orders");
            field.setAccessible(true);
            field.set(data, (Object)null);
        } catch (NoSuchFieldException var5) {
            var5.printStackTrace();
        } catch (IllegalAccessException var6) {
            var6.printStackTrace();
        }

        return new JsonResult(this.code, this.message, data);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
